package com.boot.security.server.controller;

import com.boot.security.server.dao.QuestionRecordDao;
import com.boot.security.server.mapper.MQuestionRecordMapper;
import com.boot.security.server.model.QuestionRecord;
import com.boot.security.server.model.domain.MQuestionRecord;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.user.model.SysUsers;
import com.boot.security.server.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/questionRecords")
public class QuestionRecordController {

    @Autowired
    private QuestionRecordDao questionRecordDao;
    @Autowired
    private UserService userService;
    @Autowired
    private MQuestionRecordMapper recordMapper;

    /**
     *
     **/
    @GetMapping("/{id}")
    @ApiOperation(value = "问题记录详情")
    public Map<String, Object> queryQuestionRecord (@PathVariable Integer id) {
        MQuestionRecord record = new MQuestionRecord();
        record.setOverseeQuestionId(id);
        record.setType(2);
        List<MQuestionRecord> select = recordMapper.select(record);
        select.stream().sorted(Comparator.comparing(MQuestionRecord::getCreateTime)).collect(Collectors.toList());
        for (MQuestionRecord mQuestionRecord : select) {
            SysUsers user = userService.getUser(mQuestionRecord.getUpdateUser());
            mQuestionRecord.setUpdateUser(user.getNickname());
        }
        if (select.size() == 0){
            Map<String, Object> map = new HashMap<>();
            map.put("code", 1);
            map.put("msg", "");
            map.put("count", 0);
            map.put("data", select);
            return map;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", select.size());
        map.put("data", select);
        return map;
    }

    @PostMapping
    @ApiOperation(value = "保存")
    public QuestionRecord save (@RequestBody QuestionRecord questionRecord) {
        questionRecordDao.save(questionRecord);

        return questionRecord;
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public QuestionRecord update (@RequestBody QuestionRecord questionRecord) {
        questionRecordDao.update(questionRecord);

        return questionRecord;
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public PageTableResponse list (PageTableRequest request) {

        return new PageTableHandler(new CountHandler() {

            @Override
            public int count (PageTableRequest request) {
                return questionRecordDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<QuestionRecord> list (PageTableRequest request) {
                return questionRecordDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete (@PathVariable Long id) {
        questionRecordDao.delete(id);
    }
}
